part of 'cell.dart';

class AppLabelEditCell extends StatefulWidget {
  final FocusNode? focusNode;
  final String? hint;
  final String? unit;
  final TextStyle? valueStyle;
  final TextStyle? hintStyle;
  final TextStyle? unitStyle;
  final int? maxLength;
  final Widget? title;
  final Widget? rightWidget;
  final bool? isShowBottomLine;
  final double? verticalSpacing;
  final double? horizontalSpacing;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final GestureTapCallback? onTap;
  final Color? background;
  final int? maxLines;
  final int? minLines;
  final List<TextInputFormatter>? formatters;
  final TextInputType? keyboardType;
  final ValueChanged<String>? onChanged;
  final TextEditingController? controller;
  final TextAlign textAlign;
  final Color? bottomLineColor;
  final double? bottomLineWidth;
  final bool enable;
  final Axis? axis;
  final Widget? prefix;
  final OverlayVisibilityMode prefixMode;
  final Widget? suffix;
  final OverlayVisibilityMode suffixMode;
  final Widget? clearWidget;
  final OverlayVisibilityMode clearButtonMode;

  final bool isShowCount;
  final BoxDecoration? inputDecoration;
  final EdgeInsetsGeometry? inputPadding;
  final bool obscureText;

  final TextAlign inputTextAlign;
  final bool? isAlignTop;

  AppLabelEditCell({
    super.key,
    String? title,
    double? titleWidth,
    TextStyle titleStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
    bool isMust = false,
    this.controller,
    this.hint,
    this.valueStyle,
    this.hintStyle,
    this.maxLength = 20,
    this.rightWidget,
    this.isShowBottomLine,
    this.verticalSpacing,
    this.horizontalSpacing,
    this.padding,
    this.margin,
    this.onTap,
    this.background,
    this.unit,
    this.unitStyle,
    this.maxLines,
    this.minLines,
    this.formatters,
    this.keyboardType,
    this.onChanged,
    this.bottomLineColor,
    this.bottomLineWidth,
    this.textAlign = TextAlign.end,
    this.inputTextAlign = TextAlign.start,
    this.enable = true,
    this.axis = Axis.horizontal,
    this.prefix,
    this.prefixMode = OverlayVisibilityMode.always,
    this.suffix,
    this.suffixMode = OverlayVisibilityMode.always,
    this.clearWidget,
    this.clearButtonMode = OverlayVisibilityMode.never,
    this.isShowCount = false,
    this.inputDecoration,
    this.inputPadding,
    this.focusNode,
    this.obscureText = false,
    this.isAlignTop = false,
  }) : this.title = Container(
            width: titleWidth,
            child: Text.rich(
              TextSpan(text: '', style: titleStyle, children: [
                TextSpan(
                  text: isMust ? ' * ' : '',
                  style: titleStyle.copyWith(color: Colors.red),
                ),
                TextSpan(text: title)
              ]),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ));

  AppLabelEditCell.keyHint(
      {super.key,
      String? title,
      double? titleWidth,
      TextStyle titleStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
      bool isMust = false,
      GestureTapCallback? onHintTap,
      this.controller,
      this.hint,
      this.valueStyle,
      this.hintStyle,
      this.maxLength = 20,
      this.rightWidget,
      this.isShowBottomLine,
      this.verticalSpacing,
      this.horizontalSpacing,
      this.padding,
      this.margin,
      this.onTap,
      this.background,
      this.unit,
      this.unitStyle,
      this.maxLines,
      this.minLines,
      this.formatters,
      this.keyboardType,
      this.onChanged,
      this.bottomLineColor,
      this.bottomLineWidth,
      this.enable = true,
      this.axis = Axis.horizontal,
      this.prefix,
      this.prefixMode = OverlayVisibilityMode.always,
      this.suffix,
      this.suffixMode = OverlayVisibilityMode.always,
      this.clearWidget,
      this.clearButtonMode = OverlayVisibilityMode.never,
      this.isShowCount = false,
      this.inputTextAlign = TextAlign.start,
      this.inputDecoration,
      this.inputPadding,
      this.obscureText = false,
      this.focusNode,
      this.isAlignTop = false,
      this.textAlign = TextAlign.end})
      : this.title = GestureDetector(
            onTap: onHintTap,
            child: Container(
              width: titleWidth,
              child: Text.rich(
                TextSpan(text: '', style: titleStyle, children: [
                  TextSpan(
                    text: isMust ? ' * ' : '',
                    style: titleStyle.copyWith(color: Colors.red),
                  ),
                  TextSpan(text: title)
                ]),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ));

  AppLabelEditCell.text(
      {super.key,
      String? title,
      double? titleWidth,
      TextStyle titleStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
      bool isMust = false,
      String? text,
      TextEditingController? controller,
      this.hint,
      this.valueStyle,
      this.hintStyle,
      this.maxLength = 20,
      this.rightWidget,
      this.isShowBottomLine,
      this.verticalSpacing,
      this.horizontalSpacing,
      this.padding,
      this.margin,
      this.onTap,
      this.background,
      this.unit,
      this.unitStyle,
      this.maxLines,
      this.minLines,
      this.formatters,
      this.keyboardType,
      this.onChanged,
      this.bottomLineColor,
      this.bottomLineWidth,
      this.inputTextAlign = TextAlign.start,
      this.enable = true,
      this.axis = Axis.horizontal,
      this.prefix,
      this.prefixMode = OverlayVisibilityMode.always,
      this.suffix,
      this.suffixMode = OverlayVisibilityMode.always,
      this.clearWidget,
      this.clearButtonMode = OverlayVisibilityMode.never,
      this.isShowCount = false,
      this.inputDecoration,
      this.inputPadding,
      this.obscureText = false,
      this.focusNode,
      this.isAlignTop = false,
      this.textAlign = TextAlign.end})
      : this.title = Container(
            width: titleWidth,
            child: Text.rich(
              TextSpan(text: '', style: titleStyle, children: [
                TextSpan(
                  text: isMust ? ' * ' : '',
                  style: titleStyle.copyWith(color: Colors.red),
                ),
                TextSpan(text: title)
              ]),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            )),
        this.controller = controller ??
            TextEditingController.fromValue(TextEditingValue(
                text: text ?? '', selection: TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: text?.length ?? 0))));

  @override
  State<StatefulWidget> createState() {
    return AppLabelEditCellState();
  }
}

class AppLabelEditCellState extends State<AppLabelEditCell> {
  // late TextEditingController controller;

  @override
  void initState() {
    super.initState();
    // controller = widget.controller ?? TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    Widget content = Row(
      children: [
        Expanded(
          child: AppTextField(
              focusNode: widget.focusNode,
              maxLines: widget.maxLines ?? widget.minLines ?? 1,
              minLines: widget.minLines ?? 1,
              // maxLength: widget.maxLength,
              padding: widget.inputPadding ?? const EdgeInsets.all(0.0),
              controller: widget.controller,
              style: widget.valueStyle,
              hintText: widget.hint ?? '请输入',
              hintStyle: widget.hintStyle,
              decoration: widget.inputDecoration ?? const BoxDecoration(),
              keyboardType: widget.keyboardType ?? TextInputType.text,
              inputFormatters: [TextInputLengthLimitingFormatter(widget.maxLength!), ...?widget.formatters],
              prefix: widget.prefix,
              prefixMode: widget.prefixMode,
              suffix: widget.suffix,
              suffixMode: widget.suffixMode,
              clearWidget: widget.clearWidget,
              clearButtonMode: widget.clearButtonMode,
              textAlign: widget.inputTextAlign,
              obscureText: widget.obscureText,
              onChanged: (str) {
                widget.onChanged?.call(str);
                if (mounted) {
                  setState(() {});
                }
              }),
        ),
        Container(
            alignment: Alignment.bottomRight,
            child: Text(
              widget.unit ?? '',
              style: widget.unitStyle,
            )),
        widget.axis == Axis.horizontal && widget.maxLength != null && widget.isShowCount
            ? Container(
                margin: EdgeInsets.only(left: 5.0),
                child: Text.rich(
                  TextSpan(children: [
                    TextSpan(
                        text: '${widget.controller?.text.length}',
                        style: TextStyle(color: (widget.controller?.text.length ?? 0) > widget.maxLength! ? Colors.red : Colors.grey)),
                    TextSpan(text: '/${widget.maxLength}')
                  ]),
                  style: widget.unitStyle ?? TextStyle(fontSize: 12, color: Colors.grey),
                ),
              )
            : Gaps.empty
      ],
    );
    return AppLabelCell(
      axis: widget.axis,
      enable: widget.enable,
      isAlignTop: widget.isAlignTop,
      title: widget.axis == Axis.horizontal
          ? widget.title
          : Row(
              children: [
                Expanded(child: widget.title ?? Container()),
                widget.maxLength != null && widget.isShowCount
                    ? Text.rich(
                        TextSpan(children: [
                          TextSpan(
                              text: '${widget.controller?.text.length}',
                              style: TextStyle(color: (widget.controller?.text.length ?? 0) > widget.maxLength! ? Colors.red : Colors.grey)),
                          TextSpan(text: '/${widget.maxLength}')
                        ]),
                        style: const TextStyle(fontSize: 12, color: Colors.grey),
                      )
                    : Gaps.empty,
              ],
            ),
      subTitle: content,
      bottomLineColor: widget.bottomLineColor,
      bottomLineWidth: widget.bottomLineWidth,
      isShowBottomLine: widget.isShowBottomLine ?? false,
      verticalSpacing: widget.verticalSpacing ?? 0,
      horizontalSpacing: widget.horizontalSpacing ?? 0,
      padding: widget.padding,
      margin: widget.margin,
      onTap: widget.onTap,
      background: widget.background,
      rightWidget: widget.rightWidget,
    );

    // if (widget.axis == Axis.horizontal) {
    //   return HorizontalLabelCell(
    //     enable: widget.enable,
    //     title: widget.title,
    //     subTitle: content,
    //     bottomLineColor: widget.bottomLineColor,
    //     bottomLineWidth: widget.bottomLineWidth,
    //     isShowBottomLine: widget.isShowBottomLine ?? false,
    //     verticalSpacing: widget.verticalSpacing ?? 0,
    //     horizontalSpacing: widget.horizontalSpacing ?? 0,
    //     padding: widget.padding,
    //     margin: widget.margin,
    //     onTap: widget.onTap,
    //     background: widget.background,
    //     rightWidget: widget.rightWidget,
    //   );
    // } else {
    //   return LabelCell(
    //     enable: widget.enable,
    //     subTitle: content,
    //     bottomLineColor: widget.bottomLineColor,
    //     bottomLineWidth: widget.bottomLineWidth,
    //     isShowBottomLine: widget.isShowBottomLine ?? false,
    //     verticalSpacing: widget.verticalSpacing ?? 0,
    //     horizontalSpacing: widget.horizontalSpacing ?? 0,
    //     padding: widget.padding,
    //     margin: widget.margin,
    //     onTap: widget.onTap,
    //     background: widget.background,
    //     rightWidget: widget.rightWidget,
    //   );
    // }
  }
}
